package org.apache.struts.ehr.patient_details.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts.ehr.patient_details.model.*;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.ADR_A19;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.earogya.communicate.Communicator;
import com.earogya.logic.patient_details.ADR_A19_Handler;
import com.earogya.logic.patient_details.CreateQRYA19;

public class SearchResultsAction extends ActionSupport{

	private List <PatientDetails> patientDetailsList;
	private List<Integer> patientIdList;
	private Date date;
	private String messageString;

private int selectedIndex;
	public SearchResultsAction(){
		
	}
	
	
	public SearchResultsAction(String messageString){
		this.messageString=messageString;
	}
	
	public List<PatientDetails> getPatientDetailsList() {
		return patientDetailsList;
	}
	public void setPatientDetailsList(List<PatientDetails> patientDetailsList) {
		this.patientDetailsList = patientDetailsList;
	}
	
	

	public List<Integer> getPatientIdList() {
		return patientIdList;
	}


	public void setPatientIdList(List<Integer> patientIdList) {
		this.patientIdList = patientIdList;
	}


	public int getSelectedIndex() {
		return selectedIndex;
	}


	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
	}


	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public String getMessageString() {
		return messageString;
	}
	public void setMessageString(String messageString) {
		this.messageString = messageString;
	}
	
	public String execute() throws Exception {

		ADR_A19_Handler handler=new ADR_A19_Handler(createADRA19Msg(messageString.toString()));
		patientDetailsList=handler.manageMessage();	
		patientIdList=new ArrayList<Integer>();
		for(int i=0;i<patientDetailsList.size();i++){
			patientIdList.add(patientDetailsList.get(i).getPatient().getPatientId());
		}
		return SUCCESS;
	}
	
	public ADR_A19 createADRA19Msg(String message) throws EncodingNotSupportedException, HL7Exception{
		Message hapiMsg  = new PipeParser().parse(message);
		return (ADR_A19)hapiMsg;
	}
	
	
	public void validate(){
		
	}
	
	private Boolean isInteger(String value){
		Boolean isInteger=false;
		try{
			int intValue=Integer.parseInt(value);
			isInteger=true;
		}catch(NumberFormatException e){
			isInteger=false;
		}
		return isInteger;
	}
	
	private Boolean isCorrectNICNo(String nic){
		Boolean isCorrectNicNo=false;
		int length=nic.length();
		if(length==10 && (nic.charAt(9)=='v' || nic.charAt(9)=='V')){
			isCorrectNicNo=true;
		}
		return isCorrectNicNo;
	}
	
	private Boolean isCorrectTelephoneNumber(String number){
		Boolean isCorrectNumber=false;
		String[] numbers=number.split(",");
		for(int i=0; i<numbers.length; i++){
			String num=numbers[i].trim();
			if(num.matches("[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")){
				isCorrectNumber=true;
			}else if(num.matches("[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]")){
				isCorrectNumber=true;
			}
		}
		return isCorrectNumber;
	}
	
	private Boolean isCorrectEmailAddress(String email){
		Boolean isCorrectEmail=false;
		if(email.matches("[0-9a-z]+@[a-z]+.[a-z]+")){
			isCorrectEmail=true;
		}
		return isCorrectEmail;
	}
}
